package com.spring.sunflower.main.fragment;


import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;
import com.spring.sunflower.R;
import com.spring.sunflower.base.BaseFragment;
import com.spring.sunflower.bean.BannerBean;
import com.spring.sunflower.bean.RecommendAnchorsBean;
import com.spring.sunflower.main.adapter.ImageBannerAdapter;
import com.spring.sunflower.main.adapter.LivelyAdapter;
import com.spring.sunflower.main.MainContract;
import com.spring.sunflower.main.presenter.LivelyPresenter;
import com.spring.sunflower.widget.GridDividerItemDecoration;
import com.youth.banner.Banner;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Kevin on 2021/5/28<br/>
 * <p>
 * Describe:<br/>
 */
public class LivelyFragment extends BaseFragment<LivelyPresenter> implements
        MainContract.LivelyView, OnLoadMoreListener, OnRefreshListener {
    private SmartRefreshLayout refreshLayout;
    private RecyclerView mRecyclerView;
    private List<RecommendAnchorsBean.DatasBean> mData = new ArrayList<>();
    private LivelyAdapter mAdapter;
    private Banner banner;
    private List<BannerBean.DatasBean> bannerData = new ArrayList<>();
    private ImageBannerAdapter bannerAdapter;
    private int pageIndex = 1;

    @Override
    public int setLayoutResId() {
        return R.layout.fragment_lively;
    }

    @Override
    public void initView() {
        refreshLayout = mView.findViewById(R.id.refreshLayout);
        mRecyclerView = mView.findViewById(R.id.rvRecyclerView);
        GridLayoutManager layoutManager = new GridLayoutManager(mActivity, 2);
        mRecyclerView.setLayoutManager(layoutManager);
        GridDividerItemDecoration gridDividerItemDecoration = new GridDividerItemDecoration(mActivity);
//        mRecyclerView.addItemDecoration(gridDividerItemDecoration);
        mAdapter = new LivelyAdapter(R.layout.adapter_recommend_anchors, mData);
        mAdapter.addHeaderView(getHeaderView());
        mRecyclerView.setAdapter(mAdapter);
        refreshLayout.setOnLoadMoreListener(this);
        refreshLayout.setOnRefreshListener(this);


    }

    private View getHeaderView() {
        View view = getLayoutInflater().inflate(R.layout.header_lively, (ViewGroup) mRecyclerView.getParent(), false);
        banner = view.findViewById(R.id.banner);
        bannerAdapter = new ImageBannerAdapter(mActivity, bannerData);
        banner.setAdapter(bannerAdapter);
//        banner.setBannerGalleryMZ(20);
        banner.setBannerGalleryEffect(20, 10);
        return view;
    }

    @Override
    public void initData() {

    }

    @Override
    public void initListener() {

    }

    @Override
    public void loadData() {
        mPresenter.getBanner();
        mPresenter.getRecommendAnchors(1);
    }

    @Override
    public LivelyPresenter setPresenter() {
        return new LivelyPresenter(this);
    }

    @Override
    public void showAnchorData(List<RecommendAnchorsBean.DatasBean> data) {
//        printD("mDta.size=" + mData.size());
        if (pageIndex == 1) {
            mData.clear();
            mAdapter.setList(mData);
        }
        mAdapter.addData(data);
    }

    @Override
    public void showBannerData(List<BannerBean.DatasBean> data) {
        bannerAdapter.updateData(data);
    }

    @Override
    public void liveApplySuccess() {

    }

    @Override
    public void sayHelloSuccess() {

    }

    @Override
    public void onLoadMore(@NonNull @NotNull RefreshLayout refreshLayout) {
        if (refreshLayout.isLoading()) {
            refreshLayout.finishLoadMore();
        }
        mPresenter.getRecommendAnchors(++pageIndex);
    }

    @Override
    public void onRefresh(@NonNull @NotNull RefreshLayout refreshLayout) {
        if (refreshLayout.isRefreshing()) {
            refreshLayout.finishRefresh();
        }
        pageIndex = 1;
        mPresenter.getRecommendAnchors(1);
    }
}
